package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.fragment.bussiness.BdistributionFragment;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.hdlibrary.util.fragment.ZoomOutPageTransformer;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.share.ShareCUserInfo;
import shuashuami.hb.com.view.PagerSlidingTabStrip;

public class BDistributionCenterActivity extends AbActivity {

    private ViewPager contentPager;
    private mPagerAdapter adapter;
    private PagerSlidingTabStrip tabs;

    private BdistributionFragment cdistributionFragment1;
    private BdistributionFragment cdistributionFragment2;

    private TextView tvYOngjin;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_cdistribution_center);
        contentPager = (ViewPager) findViewById(R.id.content_pager_cdistribution);
        adapter = new mPagerAdapter(getSupportFragmentManager());
        contentPager.setAdapter(adapter);
        contentPager.setOffscreenPageLimit(2);
        contentPager.setPageTransformer(true, new ZoomOutPageTransformer());

        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs_cdistribution_center);
        tabs.setTextColorResource(R.color.gray);
        tabs.setDividerColorResource(R.color.view_unpressed);
        tabs.setIndicatorColorResource(R.color.blue);
        tabs.setSelectedTextColorResource(R.color.blue);
        tabs.setIndicatorHeight(4);
        tabs.setUnderlineHeight(2);
        tabs.setTextSize(12);
        tabs.setViewPager(contentPager);
        tvYOngjin = (TextView) findViewById(R.id.tv_yongjin);

    }

    @Override
    protected void initData() {
        setTitleBar("分销中心");
        setLeftView();
        cdistributionFragment1 = new BdistributionFragment();
        cdistributionFragment1.setType("1");
        cdistributionFragment2 = new BdistributionFragment();
        cdistributionFragment2.setType("0");
        getMessage();
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class mPagerAdapter extends FragmentStatePagerAdapter {

        private String Title[] = {"已完成放单量", "未完成"};

        public mPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            // int[] id = { 5, 18, 27, 37, 21, 36, 23, 24 };
            if (arg0 == 0) {
                return cdistributionFragment1;
            } else {
                return cdistributionFragment2;
            }
        }

        @Override
        public int getCount() {
            return Title.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Title[position];
        }

    }

    private void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(BDistributionCenterActivity.this);
        HttpBMethods.getFenxiaoyongjin(listener,
                shareCUserInfo.getId()
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BDistributionCenterActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject temp = jsonObject.getJSONObject("content");
                            tvYOngjin.setText(temp.getString("money"));
                        } else {
                            ToastUtil.showShort(BDistributionCenterActivity.this, jsonObject.getString("message"));
                        }

                    } catch (JSONException e) {
                        ToastUtil.showShort(BDistributionCenterActivity.this, "数据解析错误");
                    }


                    break;
            }

            super.handleMessage(msg);
        }
    };
}
